﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vensoft.Entities;
using vensoft.BussinesLogic;

namespace vensoft.Win32
{
    public partial class frmAECliente : Form
    {
        IClienteBL clienteBL;

        public frmAECliente()
        {
            InitializeComponent();

            clienteBL = new ClienteBL();
        }

        public Cliente CurrentClient { get; set; }

        public void Editar()
        {
            clienteBindingSource.DataSource = CurrentClient;            
        }

        public void Nuevo()
        {
            Cliente currentClient = clienteBindingSource.Current as Cliente;
            clienteBindingSource.AddNew();
            currentClient = null;
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            if (rdbNatural.Checked == true)
            {
                if (dNI_RUCTextBox.Text.Trim().Length != 8)
                {
                    MessageBox.Show("El DNI ingresado no tiene 8 digitos", "Alerta");
                }
                else
                {
                    if (MessageBox.Show("¿Guardar Información?", "CONFIRMACIÓN", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                    {
                        MessageBox.Show("¡Información Guardada Satisfactoriamente!", "AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Cliente currentClient = clienteBindingSource.Current as Cliente;
                        clienteBL.RegistarCliente(currentClient);
                        this.Close();
                    }
                }
            }
            else if (rdbJuridico.Checked == true)
            {
                if (dNI_RUCTextBox.Text.Trim().Length != 11)
                {
                    MessageBox.Show("El RUC ingresado no tiene 11 digitos", "Alerta");
                }
                else
                {
                    if (MessageBox.Show("¿Guardar Información?", "CONFIRMACIÓN", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                    {
                        MessageBox.Show("¡Información Guardada Satisfactoriamente!", "AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Cliente currentClient = clienteBindingSource.Current as Cliente;
                        clienteBL.RegistarCliente(currentClient);
                        this.Close();
                    }
                }
            }
            
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmAECliente_Load(object sender, EventArgs e)
        {
            
            if (CurrentClient != null)
            {
                if (CurrentClient.tipo.Trim() == "Juridico")
                {
                    dNI_RUCTextBox.MaxLength = 11;
                    rdbJuridico.Checked = true;
                    aP_MaternoTextBox.Enabled = false;
                    aP_PaternoTextBox.Enabled = false;
                }
                else
                {
                    rdbNatural.Checked = true;
                    dNI_RUCTextBox.MaxLength = 8;
                }                
            }
            else
            {
                rdbNatural.Checked = true;
                btnEditar.Enabled = false;
            }
            
        }

        private void rdbNatural_CheckedChanged(object sender, EventArgs e)
        {
            Cliente currentClient = clienteBindingSource.Current as Cliente;    

            if (rdbNatural.Checked == true)
            {
                aP_MaternoTextBox.Enabled = true;
                aP_PaternoTextBox.Enabled = true;
                currentClient.tipo = "Natural";
                dNI_RUCTextBox.MaxLength = 8;
                //dNI_RUCTextBox.Text.Remove(8, 3);
            }
            else
            {
                aP_MaternoTextBox.Enabled = false;
                aP_PaternoTextBox.Enabled = false;

                currentClient.tipo = "Juridico";
                currentClient.AP_Materno = " ";
                currentClient.AP_Paterno = " ";
                dNI_RUCTextBox.MaxLength = 11;                
            }                                 
        }

        #region Validaciones
        private void txtSoloTexto_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txtSoloNumeros_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            { 
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
                
            }
        }
        
        private void aP_PaternoTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(aP_PaternoTextBox);
        }


        private void aP_MaternoTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(aP_MaternoTextBox);
        }


        private void razon_socialTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(razon_socialTextBox);
        }


        private void DNI_RUCTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(dNI_RUCTextBox);
        }


        private void direccionTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(direccionTextBox);
        }


        private void telefonoTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(telefonoTextBox);
        }

        private void ErrorProvider(TextBox T)
        {
            if (T.Text.Trim().Length == 0)
            {
                erpAgregarEditarCliente.SetError(T, "Debe Colocar un Dato");
                btnEditar.Enabled = false;
            }

            else
            {
                erpAgregarEditarCliente.SetError(T, "");
                btnEditar.Enabled = true;
            }
        }
        #endregion       
 
    }
}
